package com.eshop.dao;

import com.eshop.dao.provider.ProductInfoDynaSqlProvider;
import com.eshop.pojo.ProductInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;

public interface ProductInfoDao {
        @SelectProvider(type = ProductInfoDynaSqlProvider.class, method = "selectByPage")
        @Results(value = {
                @Result(id = true,property = "id",column = "id"),
                @Result(property = "type", column = "tid", one = @One (select = "com.eshop.dao.TypeDao.selectById", fetchType = FetchType.EAGER))
        })
    List<ProductInfo> selectByPage(Map<String ,Object> param);//分页获取信息

        @SelectProvider(type = ProductInfoDynaSqlProvider.class, method = "count")
    int count(Map<String ,Object> param);//根据条件查询商品总数

        @Insert("insert into Product_info(code, name, tid, brand, pic, num, price, intro, status) " +
                "values (#{code}, #{name}, #{type.id}, #{brand}, #{pic}, #{num}, #{price}, #{intro}, #{status})")
        @Options(useGeneratedKeys = true)
    void insertProduct(ProductInfo productInfo);//添加商品

        @Update("UPDATE product_info " +
                "SET code=#{code}, name=#{name}, tid=#{type.id}, brand=#{brand}, pic=#{pic}, num=#{num}, price=#{price}, intro=#{intro}, status=#{status} " +
                "WHERE id=#{id}")
    void edit(ProductInfo productInfo);//修改商品信息

        @Update("update product_info " +
                "set status = #{flag} " +
                "where id in ( ${ids} )")
    void updateStatus(@Param("ids") String ids ,@Param("flag") int flag);//跟新商品状态

        @Select("select * from product_info where status=1")
    List<ProductInfo> getOnSaleProducts();//获取所有在售商品信息

        @Select("select * from product_info where id=#{id}")
    ProductInfo selectById(int id);//根据商品id获取商品

        @Delete("delete from product_info where id in ( ${ids} )")
    int deleteProduct(@Param("ids") String ids);  //删除商品

        @Update("update product_info " +
                "set num=num-#{nums} " +
                "where id=#{productId} ")
    void sell(@Param("productId")int productId, @Param("nums")int nums);
}
